package com.datasecops.scannerservices.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.datasecops.scannerservices.enumerate.DBDrive;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * SQLServer相关操作
 *
 * @author: SunLingzhi
 * @time: 2022/8/12 16:38
 */
public class SQLServer extends DBHelper {

    private static SQLServer instance = null;

    public static Map<String, DruidDataSource> dataSourceMap = new HashMap<>();

    public static SQLServer getInstance() {
        if (null == instance) {
            synchronized (Mysql.class) {
                if (null == instance) {
                    instance = new SQLServer();
                }
            }
        }
        return instance;
    }

    public static SQLServer getInstance(String ip, int port, String user, String password,String Instance) {
        //初始化数据源
        if (null == instance) {
            synchronized (SQLServer.class) {
                if (null == instance) {
                    instance = new SQLServer();
                }
            }
        }

        //如果map没有这个数据源，则新建
        if (!SQLServer.getInstance().containsId(getSourceMapKey(ip, port,Instance))) {
            SQLServer.getInstance().addDataSource(ip, port, user, password,Instance);
        }
        //初始化数据源
        return instance;
    }

    /**
     * 获取连接
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 13:57
     */
    @Override
    public Connection getConnection(String key) {
        Connection conn = null;
        try {
            DruidDataSource source = dataSourceMap.get(key);
            conn = source.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 插入数据源
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:13
     */
    @Override
    public void addDataSource(String ip, int port, String user, String password,String Instance) {
//            String url = "jdbc:sqlserver://" + ip + ":" + port + ";DatabaseName=dsm;encrypt=false";
            String url = "jdbc:sqlserver://" + ip + ":" + port + ";encrypt=false";
        Properties prop = getCommonProp(user, password, DBDrive.SQLSERVER.getValue(), url, null);
        try {
            DruidDataSource druidDataSource = (DruidDataSource) DruidDataSourceFactory
                    .createDataSource(prop);

            putSourceMap(getSourceMapKey(ip, port,Instance), druidDataSource);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("初始化创建连接池失败！");
        }
    }

    /**
     * 判断数据源是否存在
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:09
     */
    @Override
    public boolean containsId(String id) {
        return dataSourceMap.containsKey(id);
    }

    /**
     * putMap
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:23
     */
    @Override
    public void putSourceMap(String sourceMapKey, DruidDataSource druidDataSource) {
        dataSourceMap.put(sourceMapKey, druidDataSource);
    }

    /**
     * removeMap
     *
     * @param
     * @return
     * @Description
     * @author SunLingzhi
     * @time 2022/8/15 11:23
     */
    @Override
    public void removeSourceMap(String sourceMapKey) {
        dataSourceMap.remove(sourceMapKey);
    }
}
